/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.proxy.component.HttpProxy;
	import com.edi.proxy.component.HttpProxyResult;
	import com.edi.proxy.component.SPOParam;
	
	import flash.events.MouseEvent;
	
	import mx.containers.HBox;
	import mx.controls.Button;
	import mx.controls.LinkButton;
	import mx.core.Application;
	
	/**
	 * 文件下载 
	 * 
	 */	
	public class EDIDownFile extends HBox
	{
		/**
		 * 文件编号 
		 */		
		public var refid:String="";
		/**
		 * 文件类型 
		 */		
		public var refType:String="";
		private var downList:XMLList;
		
		private var _commitProperties:Boolean=false;
		
		private var allBtnWidth:int = 0;
		private var allBtnArray:Array = new Array();
		/**
		 * 构造文件下载插件
		 * <EDIDownFile width="100%" visible="true"  height="25" refType="Document" id="downFile">
		 * </EDIDownFile>	  
		 */		
		public function EDIDownFile()
		{
			super();
		}
		
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if(_commitProperties==false)
			{
				if(Application.application.parameters.formInstanceId!=null)
				{
					refid=Application.application.parameters.formInstanceId
				}
				_commitProperties=true;
			}
		}
		
		/**
		 * 获得要下载的文件 
		 * 
		 */		
		public function build():void
		{
			if(refid!="" && refType!="")
			{
				var spo:SPOParam=new SPOParam();
				spo.serviceName="Services.serviceGetAccessory";
				spo.addParameters("refid",refid);
				spo.addParameters("appName",EDIApplication.appName);
				spo.addParameters("refType",refType);
				spo.dsName="GetAccessor";
				var proxy:HttpProxy=new HttpProxy();
				proxy.resultCallback=function(event:HttpProxyResult):void
				{
					downList=event.getPoResultByDsName("GetAccessor").table.children();
					var iCount:int=downList.length();
					var lBtn:LinkButton;
					for(var i:int=0;i<iCount;i++)
					{
						lBtn=new LinkButton();
						lBtn.label=downList[i]["caption"].toString();
						lBtn.addEventListener(MouseEvent.CLICK,downLoadFileHandler);
						lBtn.name=downList[i]["id"].toString();
						lBtn.toolTip = downList[i]["caption"].toString();
						lBtn.height=25;
						lBtn.width = 200;
						EDIApplication.outputDebug("length: "+downList[i]["caption"].toString().length);
						lBtn.visible=true;
//						allBtnArray.push(lBtn);
						addChild(lBtn);
						allBtnWidth += lBtn.width;
						if(width <= allBtnWidth){
							width += (allBtnWidth-width+50);
						}
					}
				}
				proxy.addPoParam(spo);
				proxy.send();
//				proxy.resultCallback = function(e:HttpProxyResult):void{
//					    for(var i:int=0;i<allBtnArray.length;i++){
//					    	var btn:LinkButton = (allBtnArray[i] as LinkButton);
//					    	allBtnWidth += btn.width;
//							if(width <= allBtnWidth){
//								width += (allBtnWidth-width+10);
//							}
//							EDIApplication.outputDebug("this.allBtnWidth: "+allBtnWidth);
//							EDIApplication.outputDebug("this.width: "+width);
//					    }
//				};
			}
		}
		
		/**
		 * 下载文件 
		 * @param event
		 * 
		 */		
		protected function downLoadFileHandler(event:MouseEvent):void
		{
			var id:String=String((event.currentTarget as Button).name);
			new OpenWindow((EDIApplication.path+"commonDownload?"+"?id="+id), "_blank","");
			//navigateToURL(new URLRequest(EDIApplication.path+"commonDownload?ediSessionId="+EDIApplication.sessionId+"&id="+id), "_blank");
			//ExternalInterface.call("window.open",EDIApplication.path+"commonDownload?ediSessionId="+EDIApplication.sessionId+"&id="+id,"_blank");
		}
		
		
		public function clearFiles():void{
			downList=new XMLList();
			this.removeAllChildren();
		}
		/****
		 * 解决的问题:  最后一次 addChild 后inner measure出来的值变了,从而导致inner变大了,所以就往外挤，一直把滚动条推到最外面一层。
		 * 
		 * 计算内部布局的高度的大小
		 * 添加组件后,当组件的高度和,高于当前高于当前页面高度的话,
		 * 会计算当前窗口的父级窗口的高度,如果这个高度还小于页面组件高度和的话,会继续递归外层的容器计算高度 
		 * 重载该方法,不计算父级容器的高度.
		 * 则该滚动条显示在本窗体内部
		 */
		//override protected function measure():void{};  
	}
}